package com.admission.web;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.Academic;
import com.admission.model.ArrayInfo;
import com.admission.model.Examinee;
import com.admission.model.Experience;
import com.admission.model.Family;
import com.admission.model.Resume;
import com.admission.service.ArrayInfoManager;
import com.admission.service.ExamineeManager;
import com.admission.util.NewFileName;

@Namespace("/base")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "/base/index.action", type = "redirect") })
public class ExamineeAction extends CrudActionSupport<Examinee> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2993230014124022825L;

	private ExamineeManager examineeManager;
	private Examinee examinee;
	private Academic academic;
	private String id;
	private List<Experience> experiences;
	private List<Resume> resumes;
	private List<Family> families;
	private File picture;
	private String pictureFileName;
	private String userIdentify;
	private String userPassword;
	// 标识是否跳到打印预览页面
	private String isPrintView;
	// 标识用户是否刚注册
	private String isFirstLogin;
	// 标识提交并跳回登录页面
	private String isSubmit;
	// 防刷标识
	private String isFirst;
	private ArrayInfoManager arrayInfoManager;
	private String endTime;// 报名截止显示时间

	public Examinee getModel() {
		return examinee;
	}

	@Override
	public String list() throws Exception {
		return null;
	}

	@Override
	public String input() throws Exception {
		experiences = examinee.getExperiences();
		resumes = examinee.getResumes();
		families = examinee.getFamilies();
		academic = examinee.getAcademic();
		endTime = arrayInfoManager.getByItName(ArrayInfo.SPORTENDDATE)
				.getItemValue();
		if (StringUtils.isEmpty(isPrintView)) {
			return "input";
		}
		return "print";
	}
	
	public String register() throws Exception{
		endTime = arrayInfoManager.getByItName(ArrayInfo.SPORTENDDATE)
				.getItemValue();
		return "register";
	}

	@Override
	public String save() throws Exception {
		// 这句不加也可，未明
		// examinee.setAcademic(academic);
		if (examinee.getExperiences().isEmpty()) {
			examinee.setExperiences(experiences);
		}
		if (examinee.getResumes().isEmpty()) {
			examinee.setResumes(resumes);
		}
		if (examinee.getFamilies().isEmpty()) {
			examinee.setFamilies(families);
		}
		// 头像上传
		String serverrealPath = ServletActionContext.getServletContext()
				.getRealPath("/upload");
		File dir = new File(serverrealPath);
		if (!dir.exists()) {
			dir.mkdir();
		}
		String subPath = null;
		if (picture != null) {
			subPath = new String();
			subPath = NewFileName.genNewFileName(pictureFileName, 0);
			FileUtils.copyFile(picture, new File(serverrealPath, subPath));
		}
		examineeManager.save(examinee, subPath);
		isFirst = "1";
		return this.input();
	}

	// 登录验证
	public String checkLogin() throws Exception {
		examinee = examineeManager.getByIdentity(userIdentify);
		if (examinee != null) {
			if (examinee.getPassword().trim().equals(userPassword)) {
				Struts2Utils.renderText("login" + examinee.getId());
			} else {
				Struts2Utils.renderText("密码有误");
			}
		} else {
			Struts2Utils.renderText("用户不存在");
		}
		return null;
	}

	// 判断该身份证是否已被注册 true表示不存在，可以注册，false反之
	public String checkIdentity() throws Exception {
		examinee = examineeManager.getByIdentity(userIdentify);
		if (examinee == null) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		return null;
	}

	/**
	 * 判断状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkOnlineConsult() throws Exception {
		if (examineeManager.checkConsult().equals("1")) {
			endTime = arrayInfoManager.getByItName(ArrayInfo.SPORTENDDATE)
					.getItemValue();
			return "login";
		}
		return "fail";
	}

	@Override
	public String delete() throws Exception {
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isEmpty(id)) {
			examinee = new Examinee();
		} else {
			examinee = examineeManager.getById(id);
		}
	}

	@Autowired
	public void setExamineeManager(ExamineeManager examineeManager) {
		this.examineeManager = examineeManager;
	}

	public Examinee getExaminee() {
		return examinee;
	}

	public void setExaminee(Examinee examinee) {
		this.examinee = examinee;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<Experience> getExperiences() {
		return experiences;
	}

	public Academic getAcademic() {
		return academic;
	}

	public void setAcademic(Academic academic) {
		this.academic = academic;
	}

	public List<Resume> getResumes() {
		return resumes;
	}

	public void setResumes(List<Resume> resumes) {
		this.resumes = resumes;
	}

	public List<Family> getFamilies() {
		return families;
	}

	public void setFamilies(List<Family> families) {
		this.families = families;
	}

	public void setExperiences(List<Experience> experiences) {
		this.experiences = experiences;
	}

	public File getPicture() {
		return picture;
	}

	public void setPicture(File picture) {
		this.picture = picture;
	}

	public String getPictureFileName() {
		return pictureFileName;
	}

	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}

	public void setUserIdentify(String userIdentify) {
		this.userIdentify = userIdentify;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

	public void setIsPrintView(String isPrintView) {
		this.isPrintView = isPrintView;
	}

	public String getIsFirstLogin() {
		return isFirstLogin;
	}

	public void setIsFirstLogin(String isFirstLogin) {
		this.isFirstLogin = isFirstLogin;
	}

	public String getIsSubmit() {
		return isSubmit;
	}

	public void setIsSubmit(String isSubmit) {
		this.isSubmit = isSubmit;
	}

	public String getIsFirst() {
		return isFirst;
	}

	public ArrayInfoManager getArrayInfoManager() {
		return arrayInfoManager;
	}

	@Autowired
	public void setArrayInfoManager(ArrayInfoManager arrayInfoManager) {
		this.arrayInfoManager = arrayInfoManager;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
}
